package com.qys.livesMall.app.user.service.impl;

import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.app.user.model.entity.H2AppUserBalance;
import com.qys.livesMall.app.user.model.vo.PlatformBalanceVO;
import com.qys.livesMall.app.user.model.vo.UserBalanceVO;
import com.qys.livesMall.app.user.service.IH2AppUserBalanceService;
import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.common.utils.bean.BeanProUtils;
import com.qys.livesMall.finance.model.dto.AccountDdetailSearch;
import com.qys.livesMall.finance.model.dto.AccountSearch;
import com.qys.livesMall.finance.model.entity.FinanceAccount;
import com.qys.livesMall.finance.model.entity.user.FinanceUserAccount;
import com.qys.livesMall.finance.model.entity.user.FinanceUserAccountDetail;
import com.qys.livesMall.finance.service.FinanceAccountService;
import com.qys.livesMall.finance.service.FinanceUserAccountDetailService;
import com.qys.livesMall.finance.service.FinanceUserAccountService;
import com.qys.livesMall.framework.view.QysPage;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户资金表 服务实现类
 * </p>
 *
 * @author dp
 * @since 2023-04-24
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class H2AppUserBalanceServiceImpl implements IH2AppUserBalanceService {

    private final FinanceUserAccountService financeUserAccountService;

    private final IH2AppUserService appUserService;

    private final FinanceAccountService financeAccountService;
    @Resource
    private FinanceUserAccountDetailService financeUserAccountDetailService;
    @Override
    public QysPage<UserBalanceVO> page(AccountSearch search) {
        List<H2AppUser> users=null;
        if (StringUtils.isNotBlank(search.getUserName())){
            users=appUserService.getbyUserNameLike(search.getUserName(),null);
            if (!CollectionUtils.isEmpty(users)){
                search.setUserIds(users.stream().map(H2AppUser::getId).collect(Collectors.toList()).stream().map(Long::valueOf).collect(Collectors.toList()));
            }else {
                search.setUserIds(Arrays.asList(-1L));
            }
        }
        QysPage<FinanceUserAccount> page=financeUserAccountService.list(search);
        if (CollectionUtils.isEmpty(page.getRecords())){
            return QysPage.build(search);
        }
        if (CollectionUtils.isEmpty(users)){
            List<Long> userIds = page.getRecords().stream().map(FinanceUserAccount::getUserId).collect(Collectors.toList());
            users = appUserService.listByIds(userIds);
        }
        Map<String, H2AppUser> userMap = users.stream().collect(Collectors.toMap(H2AppUser::getId, v -> v));
        List<UserBalanceVO> vos=new ArrayList<>();
        page.getRecords().forEach(e->{
            UserBalanceVO vo= BeanProUtils.copyProperties(e,UserBalanceVO::new);
            H2AppUser user=userMap.get(String.valueOf(e.getUserId()));
            if (null != user){
                vo.setUserName(user.getUserName());
                vo.setNickName(user.getNickName());
            }
            vos.add(vo);
        });
        return QysPage.build(search).setRecords(vos).setTotal(page.getTotal());
    }

    @Override
    public QysPage<PlatformBalanceVO> platformPage(AccountSearch search) {
        QysPage<FinanceAccount> page=financeAccountService.list(search);
        if (CollectionUtils.isEmpty(page.getRecords())){
            return QysPage.build(search);
        }
        List<PlatformBalanceVO> vos=BeanProUtils.copyListProperties(page.getRecords(),PlatformBalanceVO::new);
        return QysPage.build(search).setRecords(vos).setTotal(page.getTotal());
    }

    /**
     * 根据用户id查询用户账户信息
     *
     * @param userId 用户id
     */
    @Override
    public H2AppUserBalance queryByUserId(Long userId) {
        FinanceUserAccount financeUserAccount=financeUserAccountService.get(userId);
        H2AppUserBalance balance=new H2AppUserBalance();
        balance.setBalance(financeUserAccount.getTotalBalance());
        return balance;
    }

    @Override
    public List<FinanceUserAccount> listByUserIds(List<Long> userIds) {
        return financeUserAccountService.listByUserIds(userIds);
    }
}
